package ua.keeper.query;

import java.util.ArrayList;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class WeekList
{
    private SQLiteDatabase db_;
    /*****************************************************************************************/
    
    public ArrayList<WeekListItem> list = new ArrayList<WeekListItem>();
    /*****************************************************************************************/
    
    WeekList(SQLiteDatabase dataBase)
    {
    	db_ = dataBase;
    	if (!db_.isOpen())
    	{
    		Log.w("WeekList.Init()", "DataBase is not opened!");
    		return;
    	}
    	fillList();
    }
    
    /*****************************************************************************************/
    private void fillList()
    {
    	Cursor cursor = db_.rawQuery("SELECT day, SUM(q * price)" +
			" FROM clients" +
			" LEFT OUTER JOIN query_h ON query_h.client = clients.id_" +
			" LEFT OUTER JOIN query_d ON query_d.head = query_h.id_" +
			" LEFT OUTER JOIN goods ON query_d.goods = goods.id_" +
			" GROUP BY day", null);

    	if (cursor.getCount() == 0)
    	{
    		Log.w("WeekList.getList()", "Cursor is empty!");
    	}
    	cursor.moveToFirst();
    	while(!cursor.isAfterLast())
        {
    		int day_id = cursor.getInt(0);
    		// select count of clients in day
    		Cursor cursor_count = db_.rawQuery("SELECT COUNT(day) FROM clients WHERE day = ? GROUP BY day",
    				 new String[] { Integer.toString(day_id) });
    		cursor_count.moveToFirst();

    		WeekListItem item = new WeekListItem(day_id, cursor_count.getInt(0), cursor.getDouble(1));
    		list.add(item);
    		cursor.moveToNext();
    		cursor_count.close();
        }
        cursor.close();

        WeekListItem item = new WeekListItem(7, 0, 0);
        list.add(item);
    }

}

